var app = new Vue({
  el: '#app',
  data () {
    return {
      orderList: null, // 订单列表数据
      typeid: parseInt(getUrlParam("typeid")) || 1,
      types: [{
        typeid: 1,
        type: "ALL",
        name: '全部'
      }, {
        typeid: 2,
        type: "WAIT_QUERY",
        name: '待查询'
      }, {
        typeid: 3,
        type: "IN_PROGRESS",
        name: '进行中'
      }, {
        typeid: 4,
        type: "FINISHED",
        name: '已完成'
      }, {
        typeid: 5,
        type: "CANCELED",
        name: '已取消'
      }],
      transitionName: 'page-left', // 往左切换订单滑动样式class名
      curIndex: 0, // 当前下标
      curId: 0, // 当前Id
      userStatus: "", // 订单状态
      tipsText: "", // 信息提示框提示文字
      dialogShow: false, // 弹出框是否显示
      tipsShow: false, // 信息提示弹出框是否显示
      otherMarket: false, // 其它超市弹出框是否显示
      cartShow: false, // 加入购物车弹出框是否显示
      marketlist: "", // 其它超市价格弹出框数据
      num: 1, // 加入购物车弹出框商品数量
      name: "", // 其它超市价格弹出框商品名
      spreadRate: "", // 其它超市价格弹出框价差率
      errMsg: "", // Toast提示框提示文字
      currentPage: 1, // 当前页数
      pageSize: 8, // 一页显示数据条数
      totalPage: 2,
      loading: false
    }
  },computed: {
    typeTitle () {
      switch (this.typeid) {
        case 1:
          return '全部订单'
        case 2:
          return '待查询订单'
        case 3:
          return '进行中订单'
        case 4:
          return '已完成订单'
        case 5:
          return '已取消订单'
      }
    },
    typeName () {
      switch (this.typeid) {
        case 1:
          return ''
        case 2:
          return '待查询的'
        case 3:
          return '进行中的'
        case 4:
          return '已完成的'
        case 5:
          return '已取消的'
      }
    }
  },
  watch: {
    errMsg: function (newerrMsg, olderrMsg) {
      setTimeout(function() { 
        app.errMsg = ''    
      }, 2000);
    }
  },
  created () {
    this.getOrderList(this.typeid)
  },
  methods: {
    // 返回
    goback () {
      window.location.href = "../my/my.html"
    }, 
    // 全部订单
    loadMore () {// 滚动加载更多商品
        this.currentPage++
        if(this.currentPage >= this.totalPage){
            this.loading = true        
            return false
        }
        this.getOrderList(this.typeid)
    },  
    // 获取订单
    getOrderList (type) {    
      let typeName = ''
      switch (type) {
        case 1:
          typeName = 'ALL'
          break
        case 2:
          typeName = 'WAIT_QUERY'
          break
        case 3:
          typeName = 'IN_PROGRESS'
          break
        case 4:
          typeName = 'FINISHED'
          break
        case 5:
          typeName = 'CANCELED'
          break
      }
      if (typeName==='WAIT_QUERY') {
        this.getConfirmlist()
      } else {
        Axios.post('/goods/order/getOrderList.do',{
          userStatus: typeName,
          currentPage: this.currentPage,
          pageSize: this.pageSize
        }).then(res => {
          if (res.respCode === "S") {
            let list = res.data.data
            if (this.currentPage === 1) {
                this.orderList = list
            } else {
                this.orderList = this.orderList.concat(list)
            }
            this.totalPage = res.data.totalPage
            if (res.data.currentPage) {
                this.currentPage = res.data.currentPage
            }            

            if (this.currentPage < this.totalPage) {
                this.loading = false
            }
          } else {
            this.orderList = []
          }
        }).catch(err => {
          console.error(err)
        })
      }      
    },
    // 获取待查询订单
    getConfirmlist () {
        Axios.post('/self/order/selfOrder.do',{
            currentPage: this.currentPage,
            pageSize: this.pageSize
        }).then(res => {
            let list = res.data.data
            list.forEach(order => {
            order.createTime = new Date(order.createTime).Format('yy/MM/dd')
            if (order.goodsSkuList) {
                order.goodsSkuList.forEach(goods => {
                goods.minPrice = goods.minPrice / 100
                goods.maxPrice = goods.maxPrice / 100
                })
            }
            })
            if (this.currentPage === 1) {
                this.orderList = list
            } else {
                this.orderList = this.orderList.concat(list)
            }
            this.totalPage = res.data.totalPage
            this.currentPage = res.data.currentPage

            if (this.currentPage < this.totalPage) {
                this.loading = false
            }
        }).catch(err => {
            console.error(err)
        })
    },
    changeTab (typeid, type) {
      let  pageCon = document.getElementsByClassName('page-con')[0]
      pageCon.scrollTop=0
      pageCon.scrollLeft=0
      this.transitionName = typeid > this.typeid ? 'page-left' : 'page-right'
      this.typeid = typeid
      if (type==='WAIT_QUERY') {
        this.currentPage = 1
        this.getConfirmlist()
      } else {
        this.currentPage = 1
        this.getOrderList(typeid)
      }      
    },
    // 取消订单
    btnCancelOrder (index) {
      this.curIndex = index
      this.userStatus = "CANCELED"
      this.tipsText = "您确定要取消订单吗?"
      this.dialogShow = true
      this.tipsShow = true
    },
    // 确认收货
    btnConfirmlOrder (index) {
      this.curIndex = index
      this.userStatus = "FINISHED"
      this.tipsText = "您确定要确认收货吗?"
      this.dialogShow = true
      this.tipsShow = true
    },
    // 其它超市按钮
    otherPrice (index,id) {
      this.name = this.orderList[index].goodsSkuList[id].fullName
      Axios.post('/goods/goods/supermarketPriceList.do', {
        goodsSkuId: this.orderList[index].goodsSkuList[id].goodsSkuId
      }).then(res => {
        let list = res.data.supermarketGoodsList
        list.forEach(goods => {
          goods.price = goods.price / 100
        });
        this.spreadRate = res.data.spreadRate        
        this.marketlist = list
        this.dialogShow = true
        this.otherMarket = true
			}).catch(err => {
        console.error(err)
      })
    },
    // 加入购物车按钮
    cartdialogShow (index,id) {
      this.dialogShow = true
      this.cartShow = true
      this.curIndex = index
      this.curId = id
    },
    // 取消按钮
    btnCancel () {
      this.dialogShow = false
      this.tipsShow = false
      this.otherMarket = false
      this.cartShow = false
    },
    // 加号按钮
    btnPlus () {
      if (this.num<99) {
        this.num = this.num + 1
      }
    },
    btnReduce () { // 减号按钮
      if (this.num>0) {
        this.num = this.num - 1
      }
    },
    // 确定按钮
    btnOk () {
      Axios.post('/goods/order/updateOrderStatus.do',{
        ordersId: this.orderList[this.curIndex].ordersId,
        userStatus: this.userStatus
      }).then(res => {
        this.orderList[this.curIndex].status = "FINISHED"
        this.dialogShow = false
			}).catch(err => {
        console.error(err)
      })
    },
    // 加入购物车确定按钮
    btnCartOk () {
      Axios.post('/goods/shopCar/addShopCar.do', {
        goodsSkuId: this.orderList[this.curIndex].goodsSkuList[this.curId].goodsSkuId,
        quantity: this.num
      }).then(res => {        
        this.errMsg = "您已提交成功<br/>请到购物车查看"
      }).catch(err => {
        console.error(err)
      })
      this.dialogShow = false
      this.cartShow = false
      this.num = 1
    },
    // 再来一单/再次购买
    btnBuy (index) {
      Axios.post('/goods/order/againOrders.do',{
        ordersId: this.orderList[index].ordersId
      }).then(res => {
        if (res.respCode === 'S') {
          if (res.data === 'success') {
            this.errMsg = '商品已加入购物车<br>请到购物车结算！'
          } else if (res.data === 'no_price')  {
            this.errMsg = '该商品已下架，暂无价格'
          }          
        } else {
          if (res.data === 'defeat') {
            this.errMsg = '加入购物车失败！'
          }
        }
			}).catch(err => {
        console.error(err)
      })
    },
    // 跳转详情页面
    toView (id) {
      window.location.href = 'member_order_detail.html?id=' + id
    }
  }
})